package com.huanghuai.retail.controller;


import com.huanghuai.retail.common.R;
import com.huanghuai.retail.dto.AddDeliverDto;
import com.huanghuai.retail.entity.Deliver;
import com.huanghuai.retail.entity.Device;
import com.huanghuai.retail.entity.SailPlace;
import com.huanghuai.retail.service.DeviceService;
import com.huanghuai.retail.service.IDeliverService;
import com.huanghuai.retail.service.ISailPlaceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zjh
 * @since 2023-05-23
 */
@Controller
@RequestMapping("/deliver")
@Api("送货人管理")
public class DeliverController {
    @Resource
    private IDeliverService deliverService;


    @Resource
    private ISailPlaceService sailPlaceService;
    @PostMapping
    @ResponseBody
    @ApiOperation("新增送货人")
    public R<String> save(@RequestBody  @Validated  AddDeliverDto addDeliverDto){
        if(addDeliverDto == null){
            return R.error("送货人信息不能为空");
        }
        Long sailPlaceId = addDeliverDto.getSailPlaceId();

        SailPlace sailPlace = sailPlaceService.getById(sailPlaceId);
        if (sailPlace==null){
            return R.error("填写地点错误");
        }
        Deliver deliver = new Deliver();
        BeanUtils.copyProperties(addDeliverDto, deliver);
        deliverService.save(deliver);

        return R.success("添加送货人成功");
    }


    @GetMapping
    @ResponseBody
    public List<Deliver> getSailPlaceList(){

        List<Deliver> list = deliverService.list();

        return list;
    }
}
